package com.alexis.portknocking;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button; 

public class PortKnocking extends Activity {
    private static final int ACTIVITY_SETTINGS=0;
	private DBAdapter dbadapter;
	private	Handler mHandler = new Handler();
	private ProgressDialog d;
	private boolean success = true;
	
	private void launchKnocking(View view, int startstop) {
		d = ProgressDialog.show(view.getContext(), "Port knocking ...", "Please wait ...", true);
		
		final int startstop_final = startstop; // TODO: Learn how to use threads.
		
		new Thread(new Runnable() {
			public void run() {
				int[] hosts = dbadapter.getHosts(startstop_final);
				
				for (int i = 0; i < hosts.length; i++) {
					int id = hosts[i];
					final String host = dbadapter.getHost(id);
					final int port = dbadapter.getPort(id);
					mHandler.post(new Runnable() {
						public void run() {
							d.setMessage(String.format("Pinging %s:%d ...", host, port));
							Log.i("PortKnocker", String.format("Pinging %s:%d ...", host, port));
						};
					});
					
					success = true;
					if (!Knock.doKnock(host, port))
						success = false;					
				}
				
				mHandler.post(new Runnable() {
					public void run() {
						d.dismiss();
						
						endKnocking();
					}
				});
			}
		}).start();
	}
	
	public void endKnocking() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle("Port knocking");
		
		if (success) {
			alert.setMessage("Knocking complete !");
		} else {
			alert.setMessage("An error occured during knocking !");
		}
		
		alert.show();
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    	dbadapter = new DBAdapter(this.getApplicationContext());
    	dbadapter.open();
    	
    	Button startButton;
    	Button stopButton;
        
        startButton = (Button)findViewById(R.id.Button01);
        startButton.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View view) {
        		launchKnocking(view, DBAdapter.START_MODE);
        	}
        });
        
        stopButton = (Button)findViewById(R.id.Button02);
        stopButton.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View view) {
        		launchKnocking(view, DBAdapter.STOP_MODE);
        	}
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuItem menuit = menu.add(0, Menu.FIRST, 0, "Settings");
        menuit.setIcon(android.R.drawable.ic_menu_preferences);
        return true;
    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
        case Menu.FIRST:
            Intent i = new Intent(this, Settings.class);
            startActivityForResult(i, ACTIVITY_SETTINGS);
            return true;
        }
       
        return super.onMenuItemSelected(featureId, item);
    }
}